﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class QuanLyDonDatHang : System.Web.UI.Page
{
    DataTable dt = new DataTable();
    KetNoi obj = new KetNoi();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)// ko co phan nay ko update dc
        {
            KiemTraThanhToan();// Load kiemtra thanh toan
            LoadDonDatHang();// Load don dat hang 
        }
    }
    protected void KiemTraThanhToan()
    {
        dt = obj.loadDataTable("Select * from ThanhToan");
        drpkiemtrathanhtoan.DataTextField = "KiemTraThanhToan";
        drpkiemtrathanhtoan.DataValueField = "MaThanhToan";
        drpkiemtrathanhtoan.DataSource = dt;
        drpkiemtrathanhtoan.DataBind();
    }
    protected void LoadDonDatHang()
    {
        dt = obj.loadDataTable("SELECT * from DonDatHang,ThanhToan WHERE ThanhToan.MaThanhToan=DonDatHang.MaThanhToan AND ThanhToan.MaThanhToan=" + drpkiemtrathanhtoan.SelectedValue.ToString() + " ORDER BY NgayDat DESC");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        LoadDonDatHang();
        //Load drodowlist thanh toan trong gridivew
        //DropDownList drpthanhtoan = GridView1.Rows[GridView1.EditIndex].FindControl("drpthanhtoan") as DropDownList;
        //dt = obj.loadDataTable("Select * from ThanhToan");
        //drpthanhtoan.DataTextField = "KiemTraThanhToan";
        //drpthanhtoan.DataValueField = "MaThanhToan";
        //drpthanhtoan.DataSource = dt;
        //drpthanhtoan.DataBind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        LoadDonDatHang();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int madondathang = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        TextBox txtdiachigiaohang = GridView1.Rows[GridView1.EditIndex].FindControl("txtdiachigiaohang") as TextBox; ;
        string diachigiaohang = txtdiachigiaohang.Text;
        TextBox txtghichu = GridView1.Rows[GridView1.EditIndex].FindControl("txtghichu") as TextBox; ;
        string ghichu = txtghichu.Text;
        DropDownList drptinhtrangdonhang = GridView1.Rows[GridView1.EditIndex].FindControl("drptinhtrangdonhang") as DropDownList; 
        string tinhtrangdonhang = drptinhtrangdonhang.SelectedValue.ToString();
        DropDownList drpthanhtoan = GridView1.Rows[GridView1.EditIndex].FindControl("drpthanhtoan") as DropDownList;
        int mathanhtoan = Convert.ToInt32(drpthanhtoan.SelectedValue.ToString());
        //TextBox txttinhtrangdh = GridView1.Rows[GridView1.EditIndex].FindControl("txttinhtrangdh") as TextBox; ;
        //string tinhtrangdonhang = txttinhtrangdh.Text;
        obj.UpdateDonDatHang(diachigiaohang, ghichu, tinhtrangdonhang,mathanhtoan, madondathang);
        Response.Write("<script language='javascript'> alert('Cập nhật thành công !' ) </script>");
        GridView1.EditIndex = -1;
        LoadDonDatHang();
        
        //string thongtin = (GridView3.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text;
       
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int madondathang= Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        obj.XoaDonDatHang(madondathang);
        Response.Write("<script language='javascript'> alert('Xoá thành công !' ) </script>");
        LoadDonDatHang();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        LoadDonDatHang();
    }
    protected void drpkiemtrathanhtoan_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadDonDatHang();
    }
  
}
